package com.dsg.controller.account;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.account.DsgAccountBook;
import com.dsg.service.account.IDsgAccountBookService;
import com.dsg.util.SnowflakeKeyWorker;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 天鑽公司賬簿 前端控制器
 * </p>
 *
 * @author system
 * @since 2021-01-07
 */
@RestController
@RequestMapping("/account/dsgAccountBook")
public class DsgAccountBookController extends BaseController {

    @Autowired
    private IDsgAccountBookService service;

    /**
     * 查询列表（分页）
     *
     * @param dsgAccountBook
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgAccountBook dsgAccountBook) {
        // 分页
        Page<DsgAccountBook> page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("CREATE_TIME");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgAccountBook.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = service.selectByList(page, dsgAccountBook);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 新增
     *
     * @param dsgAccountBook
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgAccountBook dsgAccountBook) {
        dsgAccountBook.setOid(UUIDGenerator.getUUID());
        boolean flag = service.save(dsgAccountBook);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 批量新增
     *
     * @param dsgAccountBook
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/batchAdd")
    @ApiOperation("批量新增")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult batchAdd(@RequestBody DsgAccountBook dsgAccountBook) {
        if (CollectionUtil.isNotEmpty(dsgAccountBook.getBookList())) {
            String refernceCode = SnowflakeKeyWorker.nextStrId();
            List<DsgAccountBook> dsgAccountBookList = new ArrayList<>();
            dsgAccountBook.getBookList().forEach(I -> {
                I.setOid(UUIDGenerator.getUUID());
                I.setBookDate(dsgAccountBook.getBookDate());
                I.setRefernceCode(refernceCode);
                dsgAccountBookList.add(I);
            });
            if (CollectionUtil.isNotEmpty(dsgAccountBookList)) {
                service.saveBatch(dsgAccountBookList);
                return result(MessageConstants.SSO_STATUS_SUCCESS);
            }else{
                return setErrorJson("請填寫科目信息!");
            }
        }else{
            return setErrorJson("請填寫科目信息!");
        }
    }

    /**
     * 更新
     *
     * @param dsgAccountBook
     * @return
     */
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgAccountBook dsgAccountBook) {
        AjaxResult ajaxResult;
        boolean flag = service.updateById(dsgAccountBook);
        if (flag) {
            ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgAccountBook);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("批量删除")
    public AjaxResult delete(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList) {
        Boolean flag = service.removeByIds(Arrays.asList(idList.split(",")));
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }
}
